package ac.neec.mio.ui.activity;

import ac.neec.data.SpecificActivities;
import ac.neec.data.User;
import ac.neec.listener.DialogFragmentCallback;
import ac.neec.mio.R;
import ac.neec.mio.ui.fragment.TypeSelectorFragment;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MeasurementSettingActivity extends Activity implements DialogFragmentCallback<SpecificActivities> {
	private static final int LEVEL_MAX = 4;
	private static final int METS_MAX = 200;
	private static final String[] SPINNER_ITEM = new String[] { "50% レベル1", "60% レベル2", "70% レベル3", "75% レベル4",
			"80% レベル5" };
	private static final String[] LEVEL_ITEM = new String[] { "50%", "60%", "70%", "75%", "80%" };

	private SpecificActivities selectActivities;
	private Button selectDetailButton;
	private Button intentButton;
	private ImageButton metsPrevButton;
	private ImageButton metsNextButton;
	private TextView textType;
	private TextView textDetail;
	private TextView textMets;
	private Spinner levelSpinner;
	private TextView editMets;
	private TextView editLevel;
	private SeekBar seekMets;
	private SeekBar seekLevel;
	private View.OnClickListener mOnClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			if (v == selectDetailButton) {
				Bundle bundle = new Bundle();
				bundle.putSerializable("callback", MeasurementSettingActivity.this);
				FragmentManager manager = getFragmentManager();
				TypeSelectorFragment dialog = new TypeSelectorFragment();
				dialog.setArguments(bundle);
				dialog.show(manager, "dialog_type_selector");
			} else if (v == intentButton) {
				if (isSettingCompleate()) {
					Intent intent = new Intent(getApplicationContext(), MeasurementActivity.class);
					intent.putExtra("SpecificActivities", selectActivities);
					intent.putExtra("user", setupUserInstance());
					intent.putExtra("pointMETs", editMets.getText());
					intent.putExtra("pointLevel", editLevel.getText());
					intent.putExtra("theoryLevel", LEVEL_ITEM[levelSpinner.getSelectedItemPosition()]);
					startActivity(intent);
					finish();
				}
				else {
					Toast.makeText(getApplicationContext(), "設定を確認して下さい",
							Toast.LENGTH_SHORT).show();
				}
			}
			else if (v == metsPrevButton) {
				seekMets.setProgress(seekMets.getProgress() - 1);
			}
			else if (v == metsNextButton) {
				seekMets.setProgress(seekMets.getProgress() + 1);
			}
		}
	};

	private SeekBar.OnSeekBarChangeListener mOnSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
			if (seekBar.getId() == seekMets.getId()) {
				editMets.setText(String.valueOf(((double) progress) / 10));
			} else if (seekBar.getId() == seekLevel.getId()) {
				editLevel.setText(LEVEL_ITEM[progress]);
			}
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
		}
	};

	private AdapterView.OnItemSelectedListener mOnItemSelectedListener = new AdapterView.OnItemSelectedListener() {
		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
			seekLevel.setProgress(position);
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting_measurement);

		linkFindViews();
		setListener();
		seekInit();
	}

	private void linkFindViews() {
		selectDetailButton = (Button) findViewById(R.id.act_setting_measurement_detail_select);
		intentButton = (Button) findViewById(R.id.act_setting_measurement_intent_button);
		metsPrevButton = (ImageButton) findViewById(R.id.act_setting_measurement_mets_prev);
		metsNextButton = (ImageButton) findViewById(R.id.act_setting_measurement_mets_next);
		textType = (TextView) findViewById(R.id.act_setting_measurement_type_text);
		textDetail = (TextView) findViewById(R.id.act_setting_measurement_detail_text);
		textMets = (TextView) findViewById(R.id.act_setting_measurement_mets_text);
		levelSpinner = (Spinner) findViewById(R.id.act_setting_measurement_spinner);
		editMets = (TextView) findViewById(R.id.act_setting_measurement_mets_edit);
		editLevel = (TextView) findViewById(R.id.act_setting_measurement_level_edit);
		seekMets = (SeekBar) findViewById(R.id.act_setting_measurement_mets_seek);
		seekLevel = (SeekBar) findViewById(R.id.act_setting_measurement_level_seek);

		setSpinnerItem();
	}

	private void setSpinnerItem() {
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		adapter.addAll(SPINNER_ITEM);
		// アダプターを設定します
		levelSpinner.setAdapter(adapter);
	}

	private void setListener() {
		selectDetailButton.setOnClickListener(mOnClickListener);
		intentButton.setOnClickListener(mOnClickListener);
		metsPrevButton.setOnClickListener(mOnClickListener);
		metsNextButton.setOnClickListener(mOnClickListener);
		seekMets.setOnSeekBarChangeListener(mOnSeekBarChangeListener);
		seekLevel.setOnSeekBarChangeListener(mOnSeekBarChangeListener);
		levelSpinner.setOnItemSelectedListener(mOnItemSelectedListener);
	}

	private void seekInit() {
		seekMets.setMax(METS_MAX);
		seekMets.setProgress(METS_MAX / 2);

		seekLevel.setMax(LEVEL_MAX);
		seekLevel.setProgress(0);
	}

	private User setupUserInstance() {
		SharedPreferences pref = getSharedPreferences(
				getResources().getString(R.string.profile_preference_name), MODE_PRIVATE);
		int height = pref.getInt(getResources().getString(R.string.profile_height), -1);
		int weight = pref.getInt(getResources().getString(R.string.profile_weight), -1);
		int age = pref.getInt(getResources().getString(R.string.profile_age), -1);
		int target = pref.getInt(getResources().getString(R.string.profile_resting_heart), -1);
		String sex = pref.getString(getResources().getString(R.string.profile_sex), "NULL");

		return new User(null, height, weight, 2, age, target);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		switch (item.getItemId()) {
		case R.id.action_settings:
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void callbackData(SpecificActivities value) {
		selectActivities = value;
		textType.setText(value.getMajorHeadingTitleJP());
		textDetail.setText(value.getDetailJP());
		textMets.setText(value.getMetsString());

		seekMets.setProgress((int) (value.getMets() * 10.0F));
	}

	private boolean isSettingCompleate() {
		return (selectActivities != null) ? true : false;
	}
}
